<!-- 发送站内信 -->

<div class="modal fade" id="sendmessage" tabindex="-1" role="dialog" aria-labelledby="sendmessage">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
        
        <form target="hidden_frame" action="<?php echo Common::RewriteUrl("SendMessage","public_ajax_send_site_message");?>"   method="post">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">站内信</h4>
      </div>
      <div class="modal-body">
        <input type="hidden" class="form-control" name="to_id" readonly="readonly"/>
        <input type="hidden" class="form-control" name="to_name" readonly="readonly"/>   
         <div class="form-group input-group col-md-12 col-xs-12">   
        <div class="typeahead-container">
        <div class="typeahead-field ">

            <span class="typeahead-query">
                <input id="to_name" class="form-control" name="to_base_name" type="search" placeholder="收件人" autocomplete="off">
            </span>
           

        </div>
    </div> 
         </div>    
            
      
      <div class="form-group input-group col-md-12 col-xs-12">
         <input type="text" class="form-control" name="title" placeholder="主题"/>        
      </div>
          
      <div class="form-group input-group col-md-12 col-xs-12">
          <textarea name="content"  class="form-control" rows="6" placeholder="正文"></textarea>
      </div>
          <input type="hidden" class="form-control" name="from_id" readonly="readonly"/>   
          <input type="hidden" class="form-control" name="from_name" readonly="readonly"/>   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">发送</button>
      </div>
        
        </form>
         <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
    </div>
  </div>
</div>

<script>
    function ajax_sendmessage_callback(data){
       
        switch(data.status){
            case -1:
                tip("#to_name",data.msg,4000);
                break;
            case -4:
                tip("input[name='title']",data.msg,4000);
                break;
            case -5:
                tip("textarea[name='content']",data.msg,4000);
                break;
            case 1:
                $('#sendmessage').modal('hide');
                if(isExitsFunction("show_message_count")){
                    show_message_count(data.sendcount);
                }
                break;
        }
       
    }
    
    function isExitsFunction(funcName) {
    try {
        if (typeof(eval(funcName)) == "function") {
            return true;
        }
    } catch(e) {}
    return false;
}
    
    function  tip(tag,msg,timeout){
         $(tag).tooltip('destroy');
         $(tag).tooltip({title:msg});
         $(tag).tooltip("show");
         $(tag).focus();
         window.setTimeout(function(){$(tag).tooltip('destroy');},timeout);
    }
    
    $('#sendmessage').on('show.bs.modal', function (event) {
            $("#ajaxshowmessage").modal('hide');
            var obj = $(event.relatedTarget);
            var uid=obj.data("uid");
            var id=obj.data("id");
            var uname=obj.data("uname");
            $("input[name='from_id']").val(uid);
            $("input[name='from_name']").val(uname);
            if(id){
                $.post("<{echo Common::RewriteUrl("SendMessage","public_ajax_getmessage_json");}>",{"id":id},function(data){
                    if(data.status==1){
                         $("input[name='to_base_name']").val("@"+data.data.from_base_name);
                         $("input[name='to_id']").val(data.data.from_id);
                         $("input[name='to_name']").val(data.data.from_name);
                    }
                },"json");
            }
    });
    
    $(document).ready(function(){
        $("#to_name").typeahead({
            minLength: 1,
            hint: true,
            dynamic: true,
            template: '<span class="row">' +
        '<span class="avatar">' +
            '<img src="{{thumb}}">' +
        "</span>" +
        '<span class="username">{{base_name}}&lt;{{user_name}}&gt;</span>' +
    "</span>",
            source: {
                userlist:{
                    display: ["base_name", "user_name", "sx","pinyin"],
                    url:[{
                             type: "POST",
                             url: "<{if isset($isadmin)&&$isadmin}><{echo Common::RewriteUrl("Admin","public_ajax_tenantlist");}><{else}><{echo Common::RewriteUrl("User","public_ajax_userlist");}><{/if}>",
                             data: {
                                term:"{{query}}"
                             },
                             callback: {
                                 done:function(data){
                                     return data;
                                 }
                             }
                    },"data.userlist"]
                }
    },
    callback: {
        onClickAfter: function (node, a, item, event) {
            $("input[name='to_id']").val(item.id);
            $("input[name='to_name']").val(item.user_name);
            node.val("@"+item.base_name);
           
        },
        onResult: function (node, query, obj, objCount) {
            if(objCount==0){
                 $("input[name='to_id']").val(0);
                 $("input[name='to_name']").val("");
            }
        }
    }
    
    
        });
    });
</script>